package com.emo.wowoblog.controller;

import com.emo.wowoblog.entity.Users;
import com.emo.wowoblog.service.UserService;
import com.emo.wowoblog.utils.EmailUtils;
import com.emo.wowoblog.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    //注册时检查电子邮箱是否已经注册过
    @GetMapping("/checkEmailIsReg")
    public Map<String, Object> checkEmailIsReg(String email) {
        try {
            if (userService.isExistEmail(email)) {
                return HttpResult.success(true);
            }
            return HttpResult.success(false);

        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResult.fail();
        }
    }

    //用户注册动作
    @PostMapping("/reg")
    public Map<String, Object> reg(Users user, HttpSession session) {

        if (user.getValidateCode() == null || session.getAttribute("regValidateCode") == null) {
            System.out.println("没有提交的验证码");
            return HttpResult.fail("验证码不正确！");
        }
        //先检查验证码是否正确
        if (session.getAttribute("regValidateCode") != null) {
            String session_randomCode = (String) session.getAttribute("regValidateCode");
            System.out.println("提交的验证码是：" + user.getValidateCode());
            if (!user.getValidateCode().equals(session_randomCode)) {
                return HttpResult.fail("验证码不正确！");
            }
        }

        Users regUser = null;
        try {
            user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            user.setModifyTime(user.getCreateTime());
            userService.reg(user);
            return HttpResult.success(null, "注册成功！");

        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResult.fail();
        }
    }

    //注册时给注册邮箱发送验证码动作
    @PostMapping("/sendValidateCode")
    public Map<String, Object> sendValidateCode(String toEmail, HttpSession session) {
        try {
            String randomCode = EmailUtils.creatEmailCode();
            EmailUtils.sendEmailValidateCode(toEmail, randomCode);
            session.setAttribute("regValidateCode", randomCode);
            return HttpResult.success(null, "验证码发送成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResult.fail("验证码发送失败！");
        }
    }

    //用户登录动作
    @ApiOperation(value = "输入账号密码进行登录",notes = "输入邮箱和密码拖动验证条验证通过之后可以进行登录")
    @GetMapping({"/login", "/auth"})
    public Map<String, Object> login(String email, String password, String validateCode, String type, String jSessionId, HttpSession session) {

        try {
            Users loginUser = userService.login(email, password);
            if (loginUser != null) {
                //把登录成功的用户保存到session中
                session.setAttribute("loginUser", loginUser);
                return HttpResult.success(loginUser);
            }
            return HttpResult.fail("电子邮箱或者密码不正确。");
        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResult.fail("电子邮箱或者密码不正确。");
        }
    }

}

